package com.first.xiinn.action;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.first.core.util.ContextUtil;
import com.first.core.util.PagingBean;
import com.first.core.web.action.BaseAction;
import com.first.module.acm.model.AppUser;
import com.first.xiinn.model.InMessage;
import com.first.xiinn.model.ShortMessage;
import com.first.xiinn.service.InMessageService;
import com.first.xiinn.service.ShortMessageService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class InMessageAction extends BaseAction
{
  static short HAVE_DELETE = 1;
  private InMessage inMessage;
  private ShortMessage shortMessage;
  private Date from;
  private Date to;

  @Resource
  private InMessageService inMessageService;

  @Resource
  private ShortMessageService shortMessageService;

  public InMessage getInMessage()
  {
    return this.inMessage;
  }

  public void setInMessage(InMessage inMessage) {
    this.inMessage = inMessage;
  }
  public ShortMessage getShortMessage() {
    return this.shortMessage;
  }

  public void setShortMessage(ShortMessage shortMessage) {
    this.shortMessage = shortMessage;
  }

  public Date getFrom() {
    return this.from;
  }

  public void setFrom(Date from) {
    this.from = from;
  }

  public Date getTo() {
    return this.to;
  }

  public void setTo(Date to) {
    this.to = to;
  }

  public String list()
  {
    PagingBean pb = getInitPagingBean();
    AppUser appUser = ContextUtil.getCurrentUser();

    List list = this.inMessageService.searchInMessage(appUser.getUserId(), this.inMessage, this.shortMessage, this.from, this.to, pb);

    List<InMessage> inList = new ArrayList();
    StringBuffer buff = new StringBuffer("{success:true,'totalCounts':" + pb.getTotalItems() + ",result:");
    for (int i = 0; i < list.size(); i++) {
      InMessage inMessage = (InMessage)((Object[])list.get(i))[0];
      inList.add(inMessage);
    }
    Gson gson = new Gson();
    Type type = new TypeToken<List<InMessage>>(){}.getType();
    buff.append(gson.toJson(inList, type));
    buff.append("}");
    setJsonString(buff.toString());
    return "success";
  }

  public String know()
  {
    String strReceiveId = getRequest().getParameter("receiveId");
    String receiveId = null;
    if (StringUtils.isNotEmpty(strReceiveId)) {
      receiveId = strReceiveId;
    }
    InMessage in = (InMessage)this.inMessageService.get(receiveId);
    in.setReadFlag((short)0);
    this.inMessageService.save(in);
    setJsonString("{success:true}");
    return "success";
  }

  public String multiRemove()
  {
    String[] ids = getRequest().getParameterValues("ids");
    if (ids != null) {
      for (String id : ids) {
        this.inMessage = ((InMessage)this.inMessageService.get(id));
        this.inMessage.setDelFlag(Short.valueOf(HAVE_DELETE));
        this.inMessageService.save(this.inMessage);
      }
    }
    this.jsonString = "{success:true}";
    return "success";
  }

  public String reply()
  {
    String strReplyId = getRequest().getParameter("receiveId");
    if (StringUtils.isNotEmpty(strReplyId)) {
      String replyId = strReplyId;
      this.inMessage = ((InMessage)this.inMessageService.get(replyId));
      StringBuffer buff = new StringBuffer("{success:true,data:[");
      buff.append("{'messageId':" + this.inMessage.getShortMessage().getMessageId() + ",'senderId':'" + this.inMessage.getShortMessage().getSenderId() + "','sender':'" + this.inMessage.getShortMessage().getSender() + "'}").append("]}");
      setJsonString(buff.toString());
    } else {
      setJsonString("{success:false}");
    }
    return "success";
  }

  public String read()
  {
	String userId = ContextUtil.getCurrentUser().getUserId();
    boolean flag = false;
    if (userId != null) {
      this.inMessage = this.inMessageService.findByRead(userId);
      if (this.inMessage == null) {
        flag = true;
        this.inMessage = this.inMessageService.findLatest(userId);
      }
      if (this.inMessage != null) {
        this.inMessage.setReadFlag(InMessage.FLAG_READ);
        this.inMessageService.save(this.inMessage);
        this.shortMessage = this.inMessage.getShortMessage();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(this.shortMessage.getSendTime());
        StringBuffer buff = new StringBuffer("{success:true,message:");
        buff.append("{'receiveId':" + this.inMessage.getReceiveId() + ",'messageId':" + this.shortMessage.getMessageId() + ",'senderId':" + this.shortMessage.getSenderId() + ",'sender':'" + this.shortMessage.getSender() + "','content':'" + this.shortMessage.getContent().replace("\n", " ") + "','sendTime':'" + date );
        if (!flag) {
          InMessage in = this.inMessageService.findByRead(userId);
          if (in != null)
            buff.append(",haveNext:true");
          else
            buff.append(",haveNext:false");
        }
        else {
          buff.append(",haveNext:false");
        }
        buff.append("}}");
        setJsonString(buff.toString());
      } else {
        setJsonString("{success:false}");
      }
    } else {
      setJsonString("{success:true}");
    }
    return "success";
  }

  public String count() {
    Integer in = this.inMessageService.findByReadFlag(ContextUtil.getCurrentUser().getUserId());
    setJsonString("{success:true,count:'" + in + "'}");
    return "success";
  }

  public String detail()
  {
    String strReceiveId = getRequest().getParameter("receiveId");
    if (StringUtils.isNotEmpty(strReceiveId)) {
    	String receiveId = strReceiveId;
      this.inMessage = ((InMessage)this.inMessageService.get(receiveId));
      this.inMessage.setReadFlag((short)1);
      this.inMessageService.save(this.inMessage);
    }
    return "detail";
  }

  public String display()
  {
    PagingBean pb = new PagingBean(0, 8);
    AppUser appUser = ContextUtil.getCurrentUser();
    List list = this.shortMessageService.searchShortMessage(appUser.getUserId(), null, null, null, pb);
    List inList = new ArrayList();
    StringBuffer buff = new StringBuffer("{success:true,'totalCounts':" + pb.getTotalItems() + ",result:");
    for (int i = 0; i < list.size(); i++) {
      InMessage inMessage = (InMessage)((Object[])list.get(i))[0];
      inList.add(inMessage);
    }

    getRequest().setAttribute("messageList", inList);
    return "display";
  }

  public String multiRead() {
    String[] ids = getRequest().getParameterValues("ids");
    if (ids != null) {
      for (String id : ids) {
        this.inMessage = ((InMessage)this.inMessageService.get(id));
        this.inMessage.setReadFlag(InMessage.FLAG_READ);
        this.inMessageService.save(this.inMessage);
      }
    }
    this.jsonString = "{success:true}";
    return "success";
  }
}
